package org.example.rzfx.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import org.example.rzfx.annotation.OperationLog;
import org.example.rzfx.dto.LoginDTO;
import org.example.rzfx.dto.Result;
import org.example.rzfx.entity.User;
import org.example.rzfx.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {
    
    @Resource
    private UserService userService;
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    @OperationLog(type = "LOGIN", description = "用户登录")
    public Result<Map<String, Object>> login(@Valid @RequestBody LoginDTO loginDTO) {
        try {
            Map<String, Object> result = userService.login(loginDTO);
            return Result.success("登录成功", result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 退出登录
     */
    @PostMapping("/logout")
    @OperationLog(type = "LOGOUT", description = "用户退出")
    public Result<Void> logout() {
        userService.logout();
        return Result.success("退出成功", null);
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/current")
    public Result<User> getCurrentUser() {
        User user = userService.getCurrentUser();
        // 隐藏密码
        user.setPassword(null);
        return Result.success(user);
    }
    
    /**
     * 创建用户（仅管理员）
     */
    @PostMapping("/create")
    @SaCheckRole("1")
    @OperationLog(type = "CREATE", description = "创建用户")
    public Result<Void> createUser(@RequestBody User user) {
        try {
            userService.createUser(user);
            return Result.success("创建成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新用户状态（仅管理员）
     */
    @PutMapping("/status/{userId}/{status}")
    @SaCheckRole("1")
    public Result<Void> updateStatus(@PathVariable Long userId, @PathVariable Integer status) {
        User user = userService.getById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }
        user.setStatus(status);
        userService.updateById(user);
        return Result.success("更新成功", null);
    }
    
    /**
     * 重置密码（仅管理员）
     */
    @PutMapping("/reset-password")
    @SaCheckRole("1")
    @OperationLog(type = "UPDATE", description = "重置用户密码")
    public Result<Void> resetPassword(@RequestParam Long userId, @RequestParam String newPassword) {
        try {
            userService.resetPassword(userId, newPassword);
            return Result.success("重置成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 查询用户列表（仅管理员）
     */
    @GetMapping("/list")
    @SaCheckRole("1")
    public Result<?> listUsers() {
        return Result.success(userService.list());
    }
}

